package com.irs.irsforum.utils;

import com.irs.irsforum.entity.Resources;
import com.irs.irsforum.entity.Student;
import com.irs.irsforum.entity.Teacher;
import com.irs.irsforum.entity.User;
import com.irs.irsforum.service.IStudentService;
import com.irs.irsforum.service.ITeacherService;
import com.irs.irsforum.service.IUserService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class UserMsgUtil {
    private static final org.slf4j.Logger logger =
            LoggerFactory.getLogger(UserMsgUtil.class);

    private static IStudentService iStudentService;

    private static ITeacherService iTeacherService;

    private static IUserService iUserService;


    /**
     * userMsg为(role$id$currentTime)的加密   student$45$2020-02-12 20:19:23
     */
    public static Long processMsg(String userMsg) throws Exception {
        String privateKey = Resources.getPrivateKeyString();
        logger.info("userMsg={}, privateKey={}", userMsg, privateKey);
        String str = RSAUtil.decryptByPriKey(userMsg, privateKey);
        String[] split = str.split("\\$");
        Date date = TimeUtil.getStringToDate(split[2]);
        Long id = Long.valueOf(split[1]);
        String role = split[0];
        User user = null;
        Integer result = 0;

        logger.info("role:={} id ={} date ={}", role, id, date.toString());
        logger.info("开始进行时间检验");
        if (TimeUtil.compareDate(date, new Date()) > TimeUtil.getMinuteMillisecond(Resources.getMsgExistMinutes())) {
            return -1L;
        }

        if (role != null && role.equals(Resources.getTeacherRole())) {
            logger.info("是teacher,role={}", role);
            Teacher teacher = iTeacherService.getTeacherById(id);
            if (teacher != null) {
                if (id != null) {
                    logger.info("开始进行user是否存在检验");
                    user = new User(id, role);
                    User userByRoleOriginId = iUserService.getUserByRoleOriginId(user);
                    if (userByRoleOriginId != null) {
                        if (!userByRoleOriginId.getNickName().equals(teacher.getTeaName())) {
                            iUserService.updateUserNickName(new User(userByRoleOriginId.getId(), teacher.getTeaName(), teacher.getTeaEmail()));
                        }
                        return userByRoleOriginId.getId();
                    }
                }
                logger.info("user不存在，开始进行user的role身份验证");
                logger.info("发现该教师信息，正在录入论坛系统中.......");
                user = new User(id, role, teacher.getTeaName(), teacher.getTeaEmail(), teacher.getTeaPhone());
            } else {
                logger.error("找不到该教师信息!!");
                return 0L;
            }
        } else if (role != null && role.equals(Resources.getStudentRole())) {
            logger.info("是student,role={}", role);
            Student student = iStudentService.getStudentById(id);
            if (student != null) {
                if (id != null) {
                    logger.info("开始进行user是否存在检验");
                    user = new User(id, role);
                    User userByRoleOriginId = iUserService.getUserByRoleOriginId(user);
                    if (userByRoleOriginId != null) {
                        if (!userByRoleOriginId.getNickName().equals(student.getStuNickname())) {
                            iUserService.updateUserNickName(new User(userByRoleOriginId.getId(), student.getStuNickname(), student.getStuEmail()));
                        }
                        return userByRoleOriginId.getId();
                    }
                }
                logger.info("user不存在，开始进行user的role身份验证");
                logger.info("发现该学生信息，正在录入论坛系统中.......");
                user = new User(id, role, student.getStuNickname(), student.getStuEmail(), student.getStuPhone());
            } else {
                logger.error("找不到该学生信息!!");
                return 0L;
            }
        }
        if (user != null) {
            logger.info("完成检测判断是否合法，合法则插入user={}", user.toString());
            result = iUserService.setUser(user);
        }
        return result == 0 ? 0L : user.getId();
    }

    @Autowired
    public void setiStudentService(IStudentService iStudentService) {
        UserMsgUtil.iStudentService = iStudentService;
    }

    @Autowired
    public void setiTeacherService(ITeacherService iTeacherService) {
        UserMsgUtil.iTeacherService = iTeacherService;
    }

    @Autowired
    public void setiUserService(IUserService iUserService) {
        UserMsgUtil.iUserService = iUserService;
    }
}
